package com.calendar.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import java.util.Calendar;

import com.calendar.add.AddView;
import com.calendar.day.DayView;
import com.calendar.event.EventView;
import com.calendar.month.CalendarView;

public class CalendarWidget extends AppWidgetProvider {

	
	Calendar cal;
	
	
	public CalendarWidget(){
		cal = Calendar.getInstance();
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		
		Intent monthIntent = new Intent(context, CalendarView.class);
		PendingIntent pendingMonthIntent = PendingIntent.getActivity(context, 0, monthIntent, 0);
		
		Intent dayIntent = new Intent(context, DayView.class);
		PendingIntent pendingDayIntent = PendingIntent.getActivity(context, 0, dayIntent, 0);
		
		Intent eventIntent = new Intent(context, EventView.class);
		PendingIntent pendingEventIntent = PendingIntent.getActivity(context, 0, eventIntent, 0);
		
		Intent addIntent = new Intent(context, AddView.class);
		PendingIntent pendingAddIntent = PendingIntent.getActivity(context, 0, addIntent, 0);
		
		RemoteViews remoteView = new RemoteViews(context.getPackageName(),
				R.layout.main);
		String text = ""
				+ getWeekday()
				+ " "
				+ getMonth()
				+ " "
				+ android.text.format.DateFormat.format("dd, yyyy",
						new java.util.Date());
		remoteView.setTextViewText(R.id.Date, text);
		remoteView.setOnClickPendingIntent(R.id.MonthView, pendingMonthIntent);
		remoteView.setOnClickPendingIntent(R.id.Date, pendingDayIntent);
		remoteView.setOnClickPendingIntent(R.id.Event1, pendingEventIntent);
		remoteView.setOnClickPendingIntent(R.id.Event2, pendingEventIntent);
		remoteView.setOnClickPendingIntent(R.id.Event3, pendingEventIntent);
		remoteView.setOnClickPendingIntent(R.id.Event4, pendingEventIntent);
		remoteView.setOnClickPendingIntent(R.id.Event5, pendingEventIntent);
		remoteView.setOnClickPendingIntent(R.id.Add, pendingAddIntent);
		
		appWidgetManager.updateAppWidget(appWidgetIds, remoteView);
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		
		// v1.5 fix that doesn't call onDelete Action
		final String action = intent.getAction();
		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
			final int appWidgetId = intent.getExtras().getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				this.onDeleted(context, new int[] { appWidgetId });
			}
		} else {
			super.onReceive(context, intent);
		}
	}

	private String getWeekday() {
		String weekday = "error";
		int day = android.text.format.Time.WEEK_DAY;
		Log.i("Calendar", "Day: " + day);
		switch (day) {
		case 1:
			weekday = "Sunday";
			break;
		case 2:
			weekday = "Monday";
			break;
		case 3:
			weekday = "Tuesday";
			break;
		case 4:
			weekday = "Wednesday";
			break;
		case 5:
			weekday = "Thursday";
			break;
		case 6:
			weekday = "Friday";
			break;
		case 7:
			weekday = "Saturday";
			break;
		}
		return weekday;
	}

	private String getMonth() {
		String month = "error";
		int monthNum = cal.get(Calendar.MONTH);
		Log.i("Calendar", "Month: " + monthNum);
		switch (monthNum) {
		case 0:
			month = "January";
			break;
		case 1:
			month = "Febuary";
			break;
		case 2:
			month = "March";
			break;
		case 3:
			month = "April";
			break;
		case 4:
			month = "May";
			break;
		case 5:
			month = "June";
			break;
		case 6:
			month = "July";
			break;
		case 7:
			month = "August";
			break;
		case 8:
			month = "September";
			break;
		case 9:
			month = "October";
			break;
		case 10:
			month = "November";
			break;
		case 11:
			month = "December";
			break;
		}
		return month;
	}
}